<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="button" id="reSet" value="重新开始一局游戏">
    <div>
        <span>请输入需要输入的数字:</span>
        <input type="text" id="givenNum">
        <input type="button" id="start" value="猜">
    </div>
    <div>
        <span>已经猜的次数:</span>
        <span id="guessCount">0</span>
    </div>
    <div>
        <span>结果:</span>
        <span id="result"></span>
    </div>
    

    <script>
        let reSet = document.querySelector('#reSet');
        let givenNum = document.querySelector('#givenNum');
        let start = document.querySelector('#start');
        let guessCount = document.querySelector('#guessCount');
        let result = document.querySelector('#result');

        let guessNum = Math.floor(Math.random()*100)+1;
        console.log(guessNum);

        start.onclick = function(){
            //猜的次数+1
            let count = parseInt(guessCount.innerHTML);
            count++;
            guessCount.innerHTML = count;

            //比较猜的数字和 随机数字的大小
            if(givenNum.value < guessNum){
                result.innerHTML = '猜小了';
                result.style.color = 'red';
            }else if(givenNum.value > guessNum){
                result.innerHTML = '猜大了';
                result.style.color = 'blue';
            }else{
                result.innerHTML = '猜对了';
                result.style.color = 'purple';
            }
        }

        reSet.onclick = function(){
            guessNum = Math.floor(Math.random()*100) + 1;
            console.log(guessNum);
            givenNum.value = ''
            result.innerHTML = '';
            guessCount.innerHTML = '0';
        }
    </script>
</body>
</html>